//
//  FlagWavingController.m
//  PASystem
//
//  Created by 刘沿 on 16/9/22.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "FlagWavingController.h"
#import "FlagModel.h"
#import "FlagTableViewCell.h"
#import "FlagDetailViewController.h"


@interface FlagWavingController ()

@property (nonatomic,assign) NSInteger currentPage;

@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation FlagWavingController
static NSString *const reuserID = @"FlagCell";

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.backgroundColor = [UIColor whiteColor];
    
    __weak typeof (self) weakSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        
        [weakSelf loadMoreData];
        
    }];

}

- (void)loadData{
    
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': dqpyList,'service': PartydeptService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        [self.dataArray removeAllObjects];

        NSArray *arr = JSON[@"reason"];
        
        if (arr.count) {
            
            for (NSDictionary *dict in arr) {
                
                NSDictionary *dict2 = dict[@"attrs"];
                
                FlagModel *model = [FlagModel mj_objectWithKeyValues:dict2];
                
                [self.dataArray addObject:model];
            }
            
        }
        
        [self.tableView headerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (void)loadMoreData{
    
    self.currentPage ++;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': dqpyList,'service': PartydeptService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"reason"];
        
        if (arr.count) {
            
            for (NSDictionary *dict in arr) {
                
                NSDictionary *dict2 = dict[@"attrs"];
                
                FlagModel *model = [FlagModel mj_objectWithKeyValues:dict2];
                
                [self.dataArray addObject:model];
            }
            
        }
        
        [self.tableView footerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    FlagModel *model = self.dataArray[indexPath.row];
    
    FlagTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserID];

    if (cell == nil) {
        
        cell = [FlagTableViewCell WithFlagTableViewCell];
    }
    cell.model = model;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    FlagModel *model = self.dataArray[indexPath.row];

    FlagDetailViewController *detailVC = [[FlagDetailViewController alloc] init];

    detailVC.ID = model.ID;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}



@end













